//
//  WBEmoticonCollectionViewCell.swift
//  WBWeiBo
//
//  Created by 王斌 on 16/7/20.
//  Copyright © 2016年 王斌. All rights reserved.
//

import UIKit

//  表情视图cell
class WBEmoticonCollectionViewCell: UICollectionViewCell {
    
    // 存储20个表情按钮
    private lazy var emoticonButtons:[WBEmoticonButton] = [WBEmoticonButton]()
    
    // 绑定数据模型
    var emoticons:[WBEmoticon]?{
        didSet{
            
            guard let ets = emoticons else{
                return
            }
            // 隐藏全部按钮
            for value in emoticonButtons{
                
                value.hidden = true
            }
            
            for(i, value) in ets.enumerate(){
                
                // 获取表情按钮
                let button = emoticonButtons[i] as! WBEmoticonButton
                
                // 获取表情模型
                button.emoticon = value
                
                // 设置数据显示按钮
                button.hidden = false
                if value.type == "0"{
                    
                    // 设置图片
                    button.setImage(UIImage(named: value.path!), forState: .Normal)
                    button.setTitle(nil, forState: .Normal)
                }else{
                    
                    // 设置emoji
                    button.setTitle((value.code! as NSString).emoji(), forState: .Normal)
                    button.setImage(nil, forState: .Normal)

                }
            }
        }
    }
    
    var indexPath: NSIndexPath? {
        didSet {
            
            guard let index = indexPath else {
                return
            }
            
            messageLabel.text = "当前滚动到第\(index.section+1)组第\(index.item+1)行"
        }
    }
    
    // MARK:    --懒加载控件
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.redColor(), fontSize: 30)
        return label
        
    }()
    
    
    //  删除按钮
    private lazy var deleteEmoticonButton:UIButton = {
        
        let button = UIButton()
        
        // 添加点击事件
        button.addTarget(self, action: "deleteEmoticonButtonAction", forControlEvents: .TouchUpInside)
        
        // 设置图片
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        
        return button
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //  添加控件设置约束
    private func setupUI() {
        //  添加表情按钮
        addChildButton()
        
        // 添加删除按钮
        contentView.addSubview(deleteEmoticonButton)
        
    }
    
    
    //  添加20个表情按钮的方法
    private func addChildButton() {
        //  循环遍历20次
        for _ in 0..<20 {
            let button = WBEmoticonButton()
            button.addTarget(self, action: "emoticonButtonAction:", forControlEvents: .TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            contentView.addSubview(button)
            emoticonButtons.append(button)
        }
        
    }
    
    
    //  MARK: - 点击事件
    @objc private func deleteEmoticonButtonAction() {
        
        //  发送点击删除表情按钮的通知
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelecteDeleteEmoticonButtonNotification, object: nil)
        
    }
    
    
    @objc private func emoticonButtonAction(button: WBEmoticonButton) {
        
        let emotion = button.emoticon!
        //  发送点击表情按钮的通知,并且把表情模型传过去
        NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmotionButtonNotification, object: emotion)
        
        
    }
    //  使用layoutSubviews 给我们子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        //  计算宽度
        let itemWidth = width / 7
        //  计算高度
        let itemHeight = height / 3
        
        
        for (i, button) in emoticonButtons.enumerate() {
            //  计算列的索引
            let colIndex = i % 7
            
            //  计算行的索引
            let rowIndex = i / 7
            //  设置大小
            button.size = CGSize(width: itemWidth, height: itemHeight)
            //  设置x坐标
            button.x = CGFloat(colIndex) * itemWidth
            //  设置y坐标
            button.y = CGFloat(rowIndex) * itemHeight
            
        }
        
        // 设置删除按钮的大小
        deleteEmoticonButton.size = CGSize(width: itemWidth, height: itemHeight)
        
        // 设置x坐标
        deleteEmoticonButton.x = width - itemWidth
        
        // 设置y坐标
        deleteEmoticonButton.y = height - itemHeight
    }
}
